package compara;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class ComparatorDemo {
	public static void main(String[] args) {
		List<ComparatorEntity> list=new ArrayList<ComparatorEntity>();
		ComparatorEntity comparatorEntity1=new ComparatorEntity();
		ComparatorEntity comparatorEntity2=new ComparatorEntity();
		comparatorEntity1.setAge(2);
		comparatorEntity1.setName("b");
		comparatorEntity2.setAge(1);
		comparatorEntity2.setName("a");
		list.add(comparatorEntity2);
		list.add(comparatorEntity1);
		list.sort(new ComparatorCom());
		System.out.println(list);
	}
}
class ComparatorCom implements Comparator<ComparatorEntity>{

	@Override
	public int compare(ComparatorEntity o1, ComparatorEntity o2) {
		if(o1.getAge().compareTo(o2.getAge())>0){
			return 1;
		}
		return o1.getAge().compareTo(o2.getAge())==0?o1.getName().compareTo(o2.getName()):-1;
	}
	
}
class ComparatorEntity{
	private Integer age;
	private String name;
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "ComparatorEntity [age=" + age + ", name=" + name + "]";
	}
}
